package net.dfrz.service;

import java.util.List;

import net.dfrz.dao.IndentDao;
import net.dfrz.dao.IndentJDBCImpl;
import net.dfrz.domain.Indent;
import net.dfrz.domain.IndentOfGood;
import net.dfrz.utils.DBUtils;
import net.dfrz.utils.IndentSearchHelper;
import net.dfrz.utils.Page;
import net.dfrz.utils.TransactionManager;

public class IndentServiceImpl implements IndentService {


	public void add(Indent indent) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
    try{
			
     	IndentDao indentDao=new IndentJDBCImpl();
	    indentDao.add(indent);
		manager.commitAndClose();
			
		}catch(Exception e){
		manager.rollbackAndClose();
		}


	}


	public long countIndentByHelper(IndentSearchHelper indentSearchHelper) {
		

		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		Integer cnt=null;
		
		try{
			
				IndentDao indentDao=new IndentJDBCImpl();
		cnt=indentDao.countByHelper(indentSearchHelper);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
	
		return cnt;
	}


	public IndentOfGood getGoodsInfoFromIndent(Integer indentNo) {
		
		IndentOfGood indentOfGood=null;
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao indentDao=new IndentJDBCImpl();
			indentOfGood=indentDao.getGoodsInfo(indentNo);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return indentOfGood;
		
	}


	public List<Indent> loadAll() {
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		List<Indent> indentList=null;
		
try{
			
			IndentDao indentDao=new IndentJDBCImpl();
			indentList=indentDao.loadAll();
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return indentList;
	}


	public Page loadScopeIndent(IndentSearchHelper indentSearchHelper,Page page ) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		List<Indent> indentList=null;
		
		try{
			IndentDao indentDao=new IndentJDBCImpl();
			page.setPageContent(indentDao.loadScopeIndent(indentSearchHelper, page.getStartIndex(), page.getEndIndex()));			
			manager.commitAndClose();
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			manager.rollbackAndClose();
		}

		return page;
	}


	
	public void mend(Indent indent) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao indentDao=new IndentJDBCImpl();
			indentDao.update(indent);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
	}

	public Indent search(Integer indentNo) {
		
		Indent indent=null;
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao indentDao=new IndentJDBCImpl();
			indent=indentDao.getIndentByNo(indentNo);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		return indent;
	}


	public void updateGoodsInfoOfIndent(IndentOfGood indentOfGood) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			
			IndentDao indentDao=new IndentJDBCImpl();
			indentDao.updateGoodsInfoOfIndent(indentOfGood);
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}

	}



	public Indent getIndentByNo(String indentNo) {

		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		Indent indent=null;
		try{
			
			IndentDao indentDao=new IndentJDBCImpl();
			 indent=indentDao.getIndentByNo(Integer.parseInt(indentNo));
			manager.commitAndClose();
			
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		return indent;
	}



	public void passIndent(Integer indentNo) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			IndentDao indentDao=new IndentJDBCImpl();
			indentDao.passIndent(indentNo);
			manager.commitAndClose();
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		
	}



	public void passLessIndent(Integer indentNo) {
		
		TransactionManager manager=DBUtils.getInstance().getTransactionManager();
		manager.beginTransaction();
		
		try{
			IndentDao indentDao=new IndentJDBCImpl();
			indentDao.passLessIndent(indentNo);
			manager.commitAndClose();
		}catch(Exception e){
			manager.rollbackAndClose();
		}
		
		
	}

}
